/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yy.bugzilla.desk.tool;

import com.yy.bugzillahelp.data.BugDetailData;
import com.yy.bugzillahelp.tool.DateTool;
import com.yy.tool.ExcelTool;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/**
 *
 * @author hualun-alan
 */
public class BugListExport {

    private static final String templateName = "bug_list.xls";

    public static String export(List<BugDetailData> datas) {
        String fileName = "excel/bug_list" + DateTool.getCurrentDate() + ".xls";
        return export(datas, fileName);
    }

    public static String export(List<BugDetailData> datas, String filePath) {
        System.out.println("---------export-------------");
        System.out.println("datas=" + datas.size());
        String finalFilePath = filePath;
        try {
            HSSFWorkbook wb = new HSSFWorkbook();
            HSSFSheet sheet = wb.createSheet("Bug List");

            //<editor-fold defaultstate="collapsed" desc="write data to excel">
            int row = 1;
            int column = 1;
            for (BugDetailData bugDetailData : datas) {
                ExcelTool.setValue(sheet, row, column++, bugDetailData.getBugId());
                ExcelTool.setValue(sheet, row, column++, bugDetailData.getShortDesc());
                ExcelTool.setValue(sheet, row, column++, bugDetailData.getProduct());
                ExcelTool.setValue(sheet, row, column++, bugDetailData.getComponent());
                ExcelTool.setValue(sheet, row, column++, bugDetailData.getVersion());
                ExcelTool.setValue(sheet, row, column++, bugDetailData.getStatus());
                ExcelTool.setValue(sheet, row, column++, bugDetailData.getPriority());
                ExcelTool.setValue(sheet, row, column++, bugDetailData.getReporter());
                ExcelTool.setValue(sheet, row, column++, bugDetailData.getAssignedTo());

                column = 1;
                row++;
            }
            //</editor-fold>

            //<editor-fold defaultstate="collapsed" desc="check file exists">
            File f = new File(filePath);
            if (f.exists()) {
                int idx = filePath.lastIndexOf(".");
                String prefix = filePath.substring(0, idx);
                String suffix = filePath.substring(idx);
                int i = 1;
                finalFilePath = prefix + "(" + i++ + ")" + suffix;
                f = new File(finalFilePath);
                while (f.exists()) {
                    finalFilePath = prefix + "(" + i++ + ")" + suffix;
                    f = new File(finalFilePath);
                }
            } else {
                f.createNewFile();
            }
            //</editor-fold>

            FileOutputStream fos = new FileOutputStream(f);
            wb.write(fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(BugListExport.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(BugListExport.class.getName()).log(Level.SEVERE, null, ex);
        }

        return finalFilePath;
    }
}
